﻿Option Explicit On
Option Strict On
Imports System.Data.SqlClient

Public Class frmEmpleado
    Dim objEmpleado As New clsEmpleado
    Dim objPersona As New clsPersona
    Dim Operacion As String = ""

    'Cadena de conexión  
    Private Const cadenaconexion As String = "Data Source= .\SQLEXPRESS; Integrated Security=True; Initial Catalog=ficticia"



    Private Sub frmEmpleado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'enviar el control ComboBox y la consulta  
        cargar_ComboDNI(TipoDoc, "Select Descripcion , TipoDoc From TipoDoc")
        cargar_ComboSuc(Sucursal)
        cargar_ComboCat(Categoria)
        sub_HabilitarControles(False)
        sub_RefreshEmpleado()
    End Sub

    Private Sub cargar_ComboDNI(ByVal ComboBox As ComboBox, ByVal sql As String)
        ' nueva conexión indicando al SqlConnection la cadena de conexión  
        Dim cn As New SqlConnection(cadenaconexion)
        Try
            ' Abrir la conexión a Sql  
            cn.Open()

            ' Pasar la consulta sql y la conexión al Sql Command   
            Dim cmd As New SqlCommand(sql, cn)

            ' Inicializar un nuevo SqlDataAdapter   
            Dim da As New SqlDataAdapter(cmd)

            'Crear y Llenar un Dataset  
            Dim ds As New DataSet
            da.Fill(ds)

            ' asignar el DataSource al combobox  
            ComboBox.DataSource = ds.Tables(0)

            ' Asignar el campo a la propiedad DisplayMember del combo   
            ComboBox.DisplayMember = ds.Tables(0).Columns(0).Caption.ToString
            ComboBox.ValueMember = ds.Tables(0).Columns(1).Caption.ToString

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, _
                            "error", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        Finally
            If cn.State = ConnectionState.Open Then
                cn.Close()
            End If
        End Try
    End Sub

    Private Sub cargar_ComboSuc(ByVal ComboBox As ComboBox)
        Dim DD_Array As New ArrayList

        DD_Array.Add(New clsValueDescriptionPair("1", "158"))
        DD_Array.Add(New clsValueDescriptionPair("2", "159"))
        DD_Array.Add(New clsValueDescriptionPair("3", "201"))
        DD_Array.Add(New clsValueDescriptionPair("4", "298"))

        Try
            With ComboBox
                .DisplayMember = "Description"
                .ValueMember = "Value"
                .DataSource = DD_Array
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, _
                            "error", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cargar_ComboCat(ByVal ComboBox As ComboBox)
        Dim DD_Array As New ArrayList

        DD_Array.Add(New clsValueDescriptionPair("1", "Gerente"))
        DD_Array.Add(New clsValueDescriptionPair("2", "Jefe"))
        DD_Array.Add(New clsValueDescriptionPair("3", "Empleado"))

        Try
            With ComboBox
                .DisplayMember = "Description"
                .ValueMember = "Value"
                .DataSource = DD_Array
            End With

        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, _
                            "error", MessageBoxButtons.OK, _
                            MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub sub_RefreshEmpleado()
        With objEmpleado
            If .fn_EjecutarQueryEmpl(clsEmpleado.TipoQuery.InfoTodo) Then
                Grid.DataSource = .dbEmplDataSet.Tables("Empleado")
            Else
                MessageBox.Show(.pMsgEmpleado)
            End If
        End With
    End Sub

    Private Sub sub_RefreshPersona()
        With objPersona
            If .fn_EjecutarQuery(clsPersona.TipoQuery.InfoDesempleados) Then
                GridPersonas.DataSource = .dbPerDataSet.Tables("Persona")

            Else
                MessageBox.Show(.pMsgPersona)
            End If
        End With
    End Sub

    Private Sub sub_HabilitarControles(ByVal Valor As Boolean)
        GrpListado.Enabled = Not Valor
        GrpIndividual.Enabled = Valor

        If GrpListado.Enabled Then
            Me.Height = 280
        Else
            Me.Height = 600
        End If

    End Sub

    Private Sub Cancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelar.Click
        sub_HabilitarControles(False)
        Nuevo.Focus()
    End Sub

    Private Sub Eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Eliminar.Click
        If Grid.RowCount <= 0 Then Exit Sub
        If Grid.SelectedRows.Count = 1 Then
            Operacion = "Del"
            If MessageBox.Show("¿Esta seguro de eliminar al Empleado con legajo nro" & CInt(Grid.SelectedRows(0).Cells("Legajo").Value), "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                aceptar_Click(aceptar, New System.EventArgs)
            End If
        Else
            MessageBox.Show("Seleccione una Fila, No una celda o columna")
            sub_HabilitarControles(False)
        End If
    End Sub


    Private Sub aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles aceptar.Click
        With objEmpleado
            Select Case Operacion
                Case "Add"

                    If Trim(Legajo.Text) = "" Then
                        .pLegajo = 0
                    Else
                        .pLegajo = CInt(Trim(Legajo.Text))
                    End If

                    .pFechaIng = CDate(Trim(FechaIng.Text))

                    If Trim(Categoria.Text) = "" Then
                        .pCategoria = 0
                    Else
                        .pCategoria = CInt(Trim(Categoria.SelectedValue.ToString))
                    End If

                    If Trim(Sucursal.Text) = "" Then
                        .pSucursal = 0
                    Else
                        .pSucursal = CInt(Trim(Categoria.SelectedValue.ToString))
                    End If

                    If Trim(TipoDoc.Text) = "" Then
                        .pTipoDoc = 0
                    Else
                        .pTipoDoc = CInt(Trim(TipoDoc.SelectedValue.ToString))
                    End If

                    If Trim(NroDoc.Text) = "" Then
                        .pNroDoc = 0
                    Else
                        .pNroDoc = CInt(Trim(NroDoc.Text))
                    End If

                    If Not .fn_ExisteEmpleado(clsEmpleado.VerifExistType.Ambos) Then
                        If .fn_AddEmpleado() Then
                            sub_RefreshEmpleado()
                            sub_HabilitarControles(False)
                        Else
                            MessageBox.Show(.pMsgEmpleado)
                        End If
                    Else
                        MessageBox.Show("El empleado ya Existe.")
                    End If

                Case "Upd"

                    .pFechaIng = CDate(Trim(FechaIng.Text))
                    .pLegajo = CInt(Trim(Legajo.Text))
                    .pNroDoc = CInt(Trim(NroDoc.Text))
                    .pTipoDoc = CInt(TipoDoc.SelectedValue)
                    .pLegajo = CInt(Trim(Legajo.Text))
                    .pSucursal = CInt(Trim(Sucursal.SelectedValue.ToString))
                    .pCategoria = CInt(Trim(Categoria.SelectedValue.ToString))

                    If .fn_UpdEmpleado() Then
                        sub_RefreshEmpleado()
                        sub_HabilitarControles(False)

                    Else
                        MessageBox.Show(.pMsgEmpleado)
                    End If

                Case "Del"
                    .pLegajo = CInt(Grid.SelectedRows(0).Cells("Legajo").Value)
                    .pNroDoc = CInt(Grid.SelectedRows(0).Cells("NroDoc").Value)
                    .pTipoDoc = CInt(Grid.SelectedRows(0).Cells("TipoDoc").Value)
                    If .fn_DelEmpleado() Then
                        sub_RefreshEmpleado()
                    Else
                        MessageBox.Show(.pMsgEmpleado)

                    End If
            End Select
        End With
    End Sub


    Private Sub Modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Modificar.Click
        If Grid.RowCount <= 0 Then Exit Sub
        sub_RefreshPersona()
        aceptar.Text = "Guardar Modificación"
        Operacion = "Upd"

        With objEmpleado
            ' error cuadno no hay nada seleccionado o solo una columna
            'Dim aux As String
            'aux = Grid.SelectedRows(0)
            If Grid.SelectedRows.Count = 1 Then
                sub_HabilitarControles(True)
                .pLegajo = CInt(Grid.SelectedRows(0).Cells("Legajo").Value.ToString)

                If .fn_EjecutarQueryEmpl(clsEmpleado.TipoQuery.InfoIndividual) Then
                    NroDoc.Text = .dbEmplDataRow("NroDoc").ToString
                    TipoDoc.SelectedValue = .dbEmplDataRow("TipoDoc").ToString

                    Legajo.Text = .dbEmplDataRow("Legajo").ToString
                    FechaIng.Text = CDate(.dbEmplDataRow("FechaIngreso")).ToShortDateString
                    Sucursal.SelectedValue = .dbEmplDataRow("Sucursal").ToString
                    Categoria.SelectedValue = .dbEmplDataRow("Categoria").ToString


                    Legajo.ReadOnly = True
                    NroDoc.ReadOnly = True
                    GridPersonas.Enabled = False
                    TipoDoc.Enabled = False

                Else
                    MessageBox.Show(.pMsgEmpleado)

                End If
            Else
                MessageBox.Show("Seleccione la Fila, No una celda o columna")
                sub_HabilitarControles(False)
            End If


        End With
    End Sub

    Private Sub Nuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Nuevo.Click
        sub_RefreshPersona()
        aceptar.Text = "Guardar Nuevo"
        Operacion = "Add"
        sub_HabilitarControles(True)
        NroDoc.Text = ""
        TipoDoc.Text = ""
        FechaIng.Text = "01/01/1981"
        Sucursal.Text = ""
        Categoria.Text = ""
        Legajo.Text = ""
        NroDoc.ReadOnly = True
        TipoDoc.Enabled = False
        Legajo.ReadOnly = False
        GridPersonas.Enabled = True
        NroDoc.Focus()
    End Sub

    Private Sub btnAportes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAportes.Click

        Dim frmAporte As New frmAporte
        Me.Enabled = False
        frmAporte.MdiParent = MDI

        With objEmpleado
            If Grid.SelectedRows.Count = 1 Then
                .pLegajo = CInt(Grid.SelectedRows(0).Cells("Legajo").Value.ToString)
                frmAporte.SetEmpleado(.pLegajo)
                frmAporte.Show()
                Me.Enabled = True
            Else
                MessageBox.Show("Seleccione la Fila, No una celda o columna")
                sub_HabilitarControles(False)
                Me.Enabled = True
                sub_HabilitarControles(False)
                sub_RefreshEmpleado()
            End If
        End With

    End Sub

    Private Sub btnDescuentos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDescuentos.Click
        Dim frmDescuento As New frmDescuento
        Me.Enabled = False
        frmDescuento.MdiParent = MDI

        With objEmpleado
            If Grid.SelectedRows.Count = 1 Then
                .pLegajo = CInt(Grid.SelectedRows(0).Cells("Legajo").Value.ToString)
                frmDescuento.SetEmpleado(.pLegajo)
                frmDescuento.Show()
                Me.Enabled = True
            Else
                MessageBox.Show("Seleccione la Fila, No una celda o columna")
                sub_HabilitarControles(False)
                Me.Enabled = True
                sub_HabilitarControles(False)
                sub_RefreshEmpleado()
            End If
        End With

    End Sub

    Private Sub GridPersonas_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GridPersonas.MouseClick
        If GridPersonas.SelectedRows.Count = 1 Then
            TipoDoc.SelectedValue = GridPersonas.SelectedRows(0).Cells("TipoDoc").Value
            NroDoc.Text = GridPersonas.SelectedRows(0).Cells("NroDoc").Value.ToString
        Else
            MessageBox.Show("Seleccione la Fila, No una celda o columna")
        End If
    End Sub

End Class
